import { createRouter, createWebHistory } from 'vue-router';
import { ElMessage } from 'element-plus'; // 引入 Element Plus 的消息提示
import Login from '../views/Login.vue';
import Start from '../views/Start.vue';

const routes = [
    {
        path: '/',
        name: 'Login',
        component: Login,
    },
    {
        path: '/start',
        name: 'Start',
        component: Start,
    },
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
    const token = sessionStorage.getItem('authToken'); // 从 sessionStorage 获取 token

    // 如果要跳转到 /start 页面
    if (to.name === 'Start' && !token) {
        ElMessage.warning('当前未登录，请先登录！'); // 提示用户未登录
        next({ name: 'Login' }); // 跳转到 Login 页面
    } else {
        next(); // 继续路由
    }
});

export default router;
